import { Login } from './routes/Login';
import { LoginManager } from './LoginManager';
import { Pool } from './Pool';
import http = require('http');
import url = require('url');

var port = process.env.port || 1108;
var login = new Login();
LoginManager.getInstance();
Pool.getInstance().start();

http.createServer(function (req:http.IncomingMessage, res:http.ServerResponse)
{
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.setHeader("X-Powered-By",' 3.2.1');
    res.writeHead(200, { 'Content-Type': 'text/plain','headers':'' });
    
    let obj = url.parse(req.url);
    let pathName = obj.pathname;
    let query = obj.query;
    let send_str = "";
    let method = req.method;
    console.log('url:',req.url);
    // console.log('headers:',req.headers);
    // console.log('rawHeaders:',req.rawHeaders);
    // console.log('pathName',pathName);

    switch(pathName)
    {
        case "/login":
            console.log('get a login....');
            login.handle(pathName,req,res);
            break;
        case "/register":
            console.log('get a register....');
            login.handle(pathName,req,res);
            break;
        case "/query":
            break;
        default:
            send_str = 'no ' + pathName + ' analysis...';
            res.end(send_str);
            break;
    }
}).listen(port);

console.log(`Server Started! Please visit http://127.0.0.1:${port}`);